package com.bawei.shoppingcar.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.bawei.shoppingcar.R;
import com.bawei.shoppingcar.entity.GoodsEntity;
import com.bumptech.glide.Glide;

import java.util.List;

public class GoodsAdapter extends BaseAdapter {
    private List<GoodsEntity.DataBean> list;
    private Context context;

    public GoodsAdapter(List<GoodsEntity.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

//优化代码
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if(view == null){
            viewHolder = new ViewHolder();
            view  = LayoutInflater.from(context).inflate(R.layout.item_goods,null);
            viewHolder.iv = view.findViewById(R.id.iv);
            viewHolder.tvDesc = view.findViewById(R.id.tv_desc);
            viewHolder.tvPrice = view.findViewById(R.id.tv_price);
            viewHolder.tvStockCount = view.findViewById(R.id.tv_stock_count);
            view.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) view.getTag();
        }
        Glide.with(context).load(list.get(i).getGoods_icon()).into(viewHolder.iv);
        viewHolder.tvPrice.setText("原价：¥"+list.get(i).getGoods_price());
        viewHolder.tvDesc.setText(list.get(i).getGoods_desc());
        viewHolder.tvStockCount.setText("库存："+list.get(i).getGoods_stock_count());

        return view;
    }
    class ViewHolder{
        ImageView iv;//图片
        TextView tvDesc;//描述
        TextView tvPrice;//单价
        TextView tvStockCount;//库存
    }
}
